<?php

namespace CNEPAdmin\Model;

use Think\Model;

class SysAdminRoleModel extends Model {

    protected $_validate = array(/* array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]) */
        array('roleName', 'require', '角色名称不能为空'),
        array('roleName', '', '角色名称已存在', 0, 'unique'),
    );  // 自动验证定义
    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
        array('manType', '0', 3), /* 不允许后台管理员修改角色类型 */
    );  // 自动完成定义

    public function addRole($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    public function updateRole($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }

    public function deleteRole($roleID) {
        if ($roleID <= 0) {
            $this->error = "无效ID";
            return false;
        }
        $data['roleID'] = $roleID;
        $data['isOn'] = 0;
        $data['updateTime'] = time();
        return $this->data($data)->save();
    }

    public function getRoleById($roleID) {
        if ($roleID <= 0) {
            $this->error = "无效";
            return false;
        }
        return $this->where("roleID=$roleID")->find();
    }

    public function getRoleList($pageNo = 1, $pageSize = 15, $where = array(), $order = array()) {
        $rt = $this->where($where)->page("$pageNo,$pageSize")->order($order)->select();
        return $rt;
    }

    public function getRoleListLength($where = array()) {
        $count = $this->where($where)->count();
        return $count;
    }

}

?>